﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightClient.ServiceReferenceLogin;
using SilverlightClient.ServiceReferenceVideo;
using System.Collections.ObjectModel;

namespace SilverlightClient
{
    public partial class MainPage : UserControl
    {
        private Usuario usuario = null;
        ObservableCollection<Evento> _eventos;
        ObservableCollection<Video> _videos;
        int _indice = 0;

        public MainPage(Usuario usuarioIn)
        {
            InitializeComponent();
            usuario = usuarioIn;
            if (Utilities.EsAdminSeguidor(usuario))
            {
                btnAdministrarVideos.Visibility = Visibility.Visible;
            }
            else
            {
                btnAdministrarVideos.Visibility = Visibility.Collapsed;
            }
            _videos = new ObservableCollection<Video>();
            CargarEventos();
        }

        private void CargarEventos()
        {
            try
            {
                VideoServicesClient service = new VideoServicesClient();
                service.GetEventosCompleted += new EventHandler<GetEventosCompletedEventArgs>(VideoServicesClient_GetEventosCompleted);
                service.GetEventosAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void VideoServicesClient_GetEventosCompleted(object sender, GetEventosCompletedEventArgs e)
        {
            try
            {
                _eventos = e.Result;

                if (_eventos != null && _eventos.Count > 0)
                {
                    dataGridEventos.ItemsSource = _eventos;
                    dataGridEventos.SelectedIndex = 0;
                    VideoServicesClient service = new VideoServicesClient();
                    service.GetVideosPorEventoCompleted += new EventHandler<GetVideosPorEventoCompletedEventArgs>(VideoServicesClient_GetVideosPorEventoCompleted);
                    service.GetVideosPorEventoAsync(((Evento)dataGridEventos.SelectedItem).Id);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridEventos_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mediaPlayer.Stop();
            VideoServicesClient service = new VideoServicesClient();
            service.GetVideosPorEventoCompleted += new EventHandler<GetVideosPorEventoCompletedEventArgs>(VideoServicesClient_GetVideosPorEventoCompleted);
            service.GetVideosPorEventoAsync(((Evento)e.AddedItems[0]).Id);
        }

        void VideoServicesClient_GetVideosPorEventoCompleted(object sender, GetVideosPorEventoCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                try
                {
                    ObservableCollection<Video> videos = e.Result;
                    _videos.Clear();

                    if (videos != null && videos.Count>0)
                    {   
                        _videos = videos;
                        Uri miUri = new Uri(((Video)_videos[0]).FilePath);
                        mediaPlayer.Source = miUri;
                        mediaPlayer.Play();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }

        private void btnAdministrarVideos_Click(object sender, RoutedEventArgs e)
        {
            Grid root = Application.Current.RootVisual as Grid;
            if (root != null)
            {
                root.Children.RemoveAt(0);
                root.Children.Add(new AdministradorVideos(usuario));
            }
        }

        private void mediaPlayer_MediaEnded(object sender, RoutedEventArgs e)
        {
            _indice++;
            if (_indice == _videos.Count())
                _indice = 0;

            Uri miUri = new Uri(((Video)_videos[_indice]).FilePath);            
            mediaPlayer.Source = miUri;
        }
    }
}
